package com.kma.summer2012.client.presenters.closerequest;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.client.constants.ConstantsHolder;
import com.kma.summer2012.client.events.request.MainPageEvent;
import com.kma.summer2012.client.presenters.Presenter;
import com.kma.summer2012.client.presenters.header.HeaderPresenter;
import com.kma.summer2012.client.services.AbstractCallBack;
import com.kma.summer2012.client.services.CrumbsCatalogOfTools.CrumbsCatalogOfToolsService;
import com.kma.summer2012.client.services.request.CrumbsRequestService;
import com.kma.summer2012.client.services.session.CrumbsSessionService;
import com.kma.summer2012.shared.model.UICrumbsRequest;
import com.kma.summer2012.shared.model.UICrumbsToolItem;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Naskrina
 * Date: 16.02.13
 * Time: 14:06
 * To change this template use File | Settings | File Templates.
 */
public class CloseRequestPresenter implements Presenter {

    private UICrumbsRequest request;

    public interface Display {
        String getActualTime();

        void setActualTime(String actualTime);

        String getComment();

        void setComment(String comment);

        List<String> getToolsList();

        void setToolsList(List<String> list);

        HasClickHandlers getCloseButton();

        HasClickHandlers getCancelButton();

        Map<Integer, String> getToolList();

        Map<Integer, Integer> getAmountsValues();

        Widget asWidget();
    }

    private final Display display;

    public CloseRequestPresenter(Display display) {
        this.display = display;
        fillToolsList();   //get a list of tools from db
        bind();
        CrumbsSessionService.App.getInstance().getParameterRequest("closeRequest", new AbstractCallBack<UICrumbsRequest>() {
            @Override
            public void onSuccess(UICrumbsRequest uiCrumbsRequest) {
                CloseRequestPresenter.this.request = uiCrumbsRequest;
            }
        });
    }

    public void bind() {
        this.display.getCloseButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                doClose(request);
            }
        });
        this.display.getCancelButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                doCancel();
            }
        });
    }

    private void doClose(UICrumbsRequest request) {
        int userId = HeaderPresenter.getUser().getUserId();
        if (this.display.getComment().equals("") || this.display.getActualTime().equals("")) {
            Window.alert("Not all necessary information were filled!");
        } else {
            request.setActualTimeOfWork(Integer.parseInt(this.display.getActualTime()));
            Map<UICrumbsToolItem, Integer> actualToolsAndAmount = new HashMap<UICrumbsToolItem, Integer>();
            Map<Integer, Integer> amountValues = this.display.getAmountsValues();
            Map<Integer, String> tools = this.display.getToolList();
            for (int key : amountValues.keySet()) {
                for (UICrumbsToolItem item : this.tools) {
                    if (item.getToolName().equals(tools.get(key))) {
                        actualToolsAndAmount.put(item, amountValues.get(key));
                    }
                }
            }
            DateTimeFormat fmt = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_SERVER());
            Date date = new Date();
            CrumbsRequestService.App.getInstance().closeRequest(actualToolsAndAmount, request, this.display.getComment(), userId, fmt.format(date), new AbstractCallBack<Boolean>() {
                @Override
                public void onSuccess(Boolean aBoolean) {
                    System.out.println("Closed");
                    CrumbsEventBus.getInstance().fireEvent(new MainPageEvent());
                }
            });
        }
    }

    private void doCancel() {
        CrumbsEventBus.getInstance().fireEvent(new MainPageEvent());
    }

    private HasWidgets container;

    @Override
    public void go(HasWidgets container) {
        this.container = container;
    }

    private void showDisplay() {
        container.clear();
        container.add(display.asWidget());
    }

    List<UICrumbsToolItem> tools;

    public void fillToolsList() {

        CrumbsCatalogOfToolsService.App.getInstance().getAllTools(new AbstractCallBack<List<UICrumbsToolItem>>() {
           @Override
            public void onSuccess(List<UICrumbsToolItem> uiCrumbsToolItems) {
                tools = uiCrumbsToolItems;
                List<String> namesOfTools = new ArrayList<String>();
                for (UICrumbsToolItem item : tools) {
                    namesOfTools.add(item.getToolName());
                }
                CloseRequestPresenter.this.display.setToolsList(namesOfTools);
                CloseRequestPresenter.this.showDisplay();
            }
        });
    }
}
